<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="productOrderId" label="Order Id" />
      <el-table-column prop="reason" label="Reason" />
      <el-table-column label="IsAutomaticAudit">
        <template slot-scope="scope">
          {{ scope.row.isAutomaticAudit === 0 ? "No" : "Yes" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="Create Time" />
      <el-table-column label="Audit Status">
        <template slot-scope="scope">
          {{ auditStatusMap[scope.row.auditStatus] }}
        </template>
      </el-table-column>
      <el-table-column prop="conclusion" label="Conclusion" />
      <el-table-column prop="auditTime" label="Audit Time" />
      <el-table-column fixed="right" label="Operation" width="130">
        <template slot-scope="scope">
          <div>
            <el-button
              type="text"
              @click="showDialog(scope.row.id)"
            >Edit</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="tableData.length"
      id="btmPagination"
      :class="{ open: sidebar }"
      :current-page="currentPage"
      :page-sizes="[10, 30, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 新增/编辑分类弹窗 -->
    <el-dialog
      title="Edit"
      :visible.sync="dialogFormVisible"
      width="400px"
      @close="closeDialog('ruleForm')"
    >
      <div v-loading="dialogLoading" class="dialogLoadingBox">
        <el-form
          ref="ruleForm"
          :model="dialogForm"
          :rules="rules"
          :label-width="formLabelWidth"
        >
          <el-form-item prop="name" label="Reason">
            <el-input v-model="dialogForm.reason" type="textarea" />
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog('ruleForm')">Cancle</el-button>
          <el-button
            type="primary"
            @click="submitData('ruleForm')"
          >Confirm</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { appealPage, appealRead, appealEdit } from '@/api/appeal';
import { postResult } from '@/utils/dataFormat';
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogFormVisible: false,
      dialogForm: {},
      dialogLoading: false,
      rules: {
        reason: [
          {
            message: 'Please enter the reason',
            required: true,
            trigger: 'blur'
          }
        ]
      },
      formLabelWidth: '130px',
      auditStatusMap: {
        0: 'UNAUDITED',
        1: 'AUDITED',
        2: 'AUDITED_FAIL'
      }
    };
  },
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar.opened;
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.loading = true;
      appealPage({
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        this.tableData = res.data.data;
        this.total = res.data.totalCount;
        this.loading = false;
      });
    },
    showDialog(id) {
      this.dialogFormVisible = true;
      this.dialogLoading = true;
      appealRead(id).then(res => {
        this.dialogLoading = false;
        this.dialogForm = res.data;
      });
    },
    submitData(ruleForm) {
      this.$refs[ruleForm].validate(async valid => {
        if (valid) {
          const { id, reason } = this.dialogForm;
          appealEdit({ id, reason }).then(res => {
            this.dialogFormVisible = false;
            postResult(res, this.$message, this.init);
          });
        } else {
          return false;
        }
      });
    },
    closeDialog(ruleForm) {
      this.dialogFormVisible = false;
      this.$refs[ruleForm].resetFields();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.init();
    }
  }
};
</script>
<style lang="scss" scoped></style>
